package cn.itcast.java.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Author itcast
 * Date 2021/10/10 15:19
 * Desc TODO
 */
public class CollectionDemo03 {
    public static void main(String[] args) {
        Student zhangsan = new Student("zhangsan", 20);
        Student wangwu = new Student("zhangsan", 20);
        System.out.println(zhangsan.hashCode() == wangwu.hashCode());
        //定义集合
        Collection<Student> students = new ArrayList<>();
        students.add(zhangsan);
        students.add(wangwu);
        //打印输出结果
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    public static class Student {
        private String name;
        private int age;

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return this.name + "的年龄是：" + this.age;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Student student = (Student) o;

            if (age != student.age) return false;
            return name != null ? name.equals(student.name) : student.name == null;
        }

        @Override
        public int hashCode() {
            int result = name != null ? name.hashCode() : 0;
            result = 31 * result + age;
            return result;
        }
    }
}
